package com.jingwei.controllers;

import java.util.Date;
import java.util.List;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;

import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import com.jingwei.bean.User;
import com.jingwei.dao.UserDAO;

public class HelloController {

	@Autowired
	private UserDAO userDAO;

	public Object world(@Param("name") String name, Invocation inv) {
		// User user = new User();
		// user.setName("david");
		// user.setAge(29);
		// userDAO.save(user);

		User user = userDAO.findByName(name);

		inv.addModel("date", new Date());

		JSONObject json = new JSONObject();

		if (user != null) {
			inv.addModel("user", user);
			json.put("name", user.getName());
			json.put("age", user.getAge());
		}

		return json;
	}

	public String show(Invocation inv) {
		List<User> list = userDAO.find(10);
		inv.addModel("userList", list);
		inv.addModel("root", System.getProperty("xiaonei_widget.root"));
		return "hello-show";
	}

	public Object add(Invocation inv) {
		User user = new User();
		user.setName("david");
		user.setAge(29);
		userDAO.save(user);

		return "hello-show";
	}

}
